package com.ziyun.pop.mall.module.user.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.service.common.model.PageResult;
import com.service.ucenter.api.UserMessageService;
import com.service.ucenter.constant.UcenterResult;
import com.service.ucenter.dao.UserMessageDao;
import com.service.ucenter.model.message.MessageParamDto;
import com.service.ucenter.model.message.MessageResultDto;
import com.service.ucenter.model.message.MessageVO;
import com.service.ucenter.module.message.entity.UserMessage;
import com.service.utils.push.umeng.JsonResult;
import com.ziyun.pop.mall.system.config.HttpRequestLocal;

/**
 * 用户消息Controller
 * 
 * @author lvai 10.19
 */
@Controller
public class UserMessageController {
	private static Logger _log = LoggerFactory.getLogger(UserMessageController.class);
	private static final String MODEL = "/user/userMessage";

	@Autowired
	private UserMessageService userMessageService;

	@Autowired
	private HttpRequestLocal httpRequestLocal;

	/**
	 * 查看我的消息（全部、未读）
	 * 
	 * @return
	 * @throws IOException
	 */
	@GetMapping(MODEL + "/index.do")
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response) throws IOException {
		ModelAndView view = new ModelAndView("/user/userMessage/index.html");
		String pageNo = request.getParameter("pageNo");
		view.addObject("pageNo", pageNo);
		return view;
	}

	/**
	 * 查看我的消息（全部、未读）
	 * 
	 * @return
	 */
	@PostMapping(MODEL + "/loadMessage.json")
	@ResponseBody
	public JsonResult<Object> loadMessage(MessageParamDto messageParamDto) {
		if (messageParamDto.getType() == null) {
			messageParamDto.setType(0);
		}
		UcenterResult<MessageResultDto> ucenterResult = userMessageService.loadMessage(messageParamDto);
		PageResult<MessageVO> messageList = ucenterResult.getData().getMessageList();
		return JsonResult.success(messageList);
	}

	/**
	 * 查看我的消息内容
	 * 
	 * @return
	 * @throws IOException
	 */
	@GetMapping(MODEL + "/readMessage.do")
	public ModelAndView readMessage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String messageId = request.getParameter("messageId");
		UserMessage userMessage = UserMessageDao.instance.selectById(messageId);
		if (!userMessage.getReadStatus()) {// 如果未读，设为已读
			userMessageService.saveMessageStatus(Integer.parseInt(messageId));
		}
		ModelAndView view = new ModelAndView("/user/userMessage/readMessage.html");
		view.addObject("messageId", messageId);
		return view;
	}

	/**
	 * 根据Id获取消息内容
	 * 
	 * @return
	 * @throws IOException
	 */
	@PostMapping(MODEL + "/getUserMessageByMessageId.json")
	@ResponseBody
	public JsonResult<UserMessage> getUserMessageByMessageId(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		String messageId = request.getParameter("messageId");
		UserMessage userMessage = UserMessageDao.instance.selectById(messageId);
		return JsonResult.success(userMessage);
	}

	/**
	 * 删除消息
	 * 
	 * @return
	 */
	@PostMapping(MODEL + "/delMessage.json")
	@ResponseBody
	public JsonResult<UcenterResult<?>> delMessage(String messageIds) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("messageIds", messageIds);
		UcenterResult<?> ucenterResult = userMessageService.delMessage(map);
		return JsonResult.success(ucenterResult);
	}

	/**
	 * 一键清空消息
	 * 
	 * @return
	 */
	@PostMapping(MODEL + "/clearMessage.json")
	@ResponseBody
	public JsonResult<?> clearMessage() {
		userMessageService.clearMessage();
		return JsonResult.success();
	}

	/**
	 * 未读消息数量
	 * 
	 * @return
	 */
	@PostMapping(MODEL + "/unMessageCount.json")
	@ResponseBody
	public JsonResult<UcenterResult<Integer>> unMessageCount() {
		Long userId = (Long) httpRequestLocal.getSessionValue("userId");
		UcenterResult<Integer> ucenterResult = new UcenterResult<Integer>();
		if (userId == null) {
			ucenterResult.setCode(9999);
			return JsonResult.success(ucenterResult);
		} else {
			ucenterResult = userMessageService.unMessageCount();
			return JsonResult.success(ucenterResult);
		}
	}

	/**
	 * 修改阅读状态
	 * 
	 * @return
	 */
	@PostMapping(MODEL + "/saveMessageStatus.json")
	@ResponseBody
	public JsonResult<UcenterResult<?>> saveMessageStatus(Integer messageId) {
		UcenterResult<?> ucenterResult = userMessageService.saveMessageStatus(messageId);
		return JsonResult.success(ucenterResult);
	}

	/**
	 * 批量修改阅读状态
	 * 
	 * @return
	 */
	@PostMapping(MODEL + "/saveBatchMessageStatus.json")
	@ResponseBody
	public JsonResult<UcenterResult<?>> saveBatchMessageStatus(String messageIds) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("messageIds", messageIds);
		UcenterResult<?> ucenterResult = userMessageService.saveBatchMessageStatus(map);
		return JsonResult.success(ucenterResult);
	}

}
